package com.example.yunJie.digitalSentry.service.impl;

import com.example.yunJie.digitalSentry.entity.ClockEntity;
import com.example.yunJie.digitalSentry.mapper.one.ClockMapper;
import com.example.yunJie.digitalSentry.service.ClockService;
import com.example.yunJie.digitalSentry.util.TimeDifference;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ClockServiceImpl implements ClockService {

    private final ClockMapper clockMapper;

    @Override
    public int insert(ClockEntity clockEntity) {
        return clockMapper.insert(clockEntity);
    }

    @Override
    public int update(ClockEntity clockEntity) {
        return clockMapper.update(clockEntity);
    }

    @Override
    public int deleteById(Serializable id) {
        return clockMapper.deleteById(id);
    }

    @Override
    public int deleteIdList(List<Integer> idList) {
        return clockMapper.deleteIdList(idList);
    }

    @Override
    public ClockEntity findId(Serializable id) {
        return clockMapper.findId(id);
    }

    @Override
    public List<ClockEntity> findAll(ClockEntity clockEntity) throws ParseException {
        SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
        List<ClockEntity> all = clockMapper.findAll(clockEntity);
        for (ClockEntity clockEntity1 :all){
            List<ClockEntity> byPersonalNameAndDate = clockMapper.findByPersonalNameAndDate(clockEntity1.getPersonalName(), sdf2.format(clockEntity1.getClockingTime()));
                if (clockEntity1.getLateTime() == null || clockEntity1.getWorkOvertime() == null){
                    if (byPersonalNameAndDate.size()==1){
                        if (byPersonalNameAndDate.get(0).getClockingTime().equals(clockEntity1.getClockingTime())){
                            if (byPersonalNameAndDate.get(0).getShiftName() != null){
                                Date clockingTime = byPersonalNameAndDate.get(0).getClockingTime();
                                String format = sdf1.format(clockingTime);
                                StringBuilder sb = new StringBuilder();
                                sb.append(format.substring(0,10));
                                sb.append(" ");
                                sb.append(byPersonalNameAndDate.get(0).getStartTime());
                                Date parse = sdf1.parse(sb.toString());
                                String shangWu = TimeDifference.getDatePoor(parse, clockingTime);
                                int i = Integer.parseInt(shangWu);
                                if (i>0){
                                    clockEntity1.setLateTime("0");
                                }else {
                                    int abs = Math.abs(i);
                                    clockEntity1.setLateTime(String.valueOf(abs));
                                }
                            }
                        }
                    }else if (byPersonalNameAndDate.size()>=2){

                        if (byPersonalNameAndDate.get(byPersonalNameAndDate.size()-1).getEndTime() != null){
                            Date clockingTime = byPersonalNameAndDate.get(0).getClockingTime();
                            String format = sdf1.format(clockingTime);
                            StringBuilder sb = new StringBuilder();
                            sb.append(format.substring(0,10));
                            sb.append(" ");
                            sb.append(byPersonalNameAndDate.get(0).getStartTime());
                            Date parse = sdf1.parse(sb.toString());
                            String shangWu = TimeDifference.getDatePoor(parse, clockingTime);
                            int i = Integer.parseInt(shangWu);
                            if (i>0){
                                clockEntity1.setLateTime("0");
                            }else {
                                int abs = Math.abs(i);
                                clockEntity1.setLateTime(String.valueOf(abs));
                            }

                            Date clockingTime1 = byPersonalNameAndDate.get(byPersonalNameAndDate.size()-1).getClockingTime();
                            String format1 = sdf1.format(clockingTime1);
                            StringBuilder sb1 = new StringBuilder();
                            sb1.append(format1.substring(0,10));
                            sb1.append(" ");
                            sb1.append(byPersonalNameAndDate.get(byPersonalNameAndDate.size()-1).getEndTime());
                            Date parse1 = sdf1.parse(sb1.toString());
                            String xiaWu = TimeDifference.getDatePoor(parse1, clockingTime1);
                            int j = Integer.parseInt(xiaWu);
                            if (j>0){
                                clockEntity1.setLeaveEarly(String.valueOf(Math.abs(j)));
                            }else {
                                int abs = Math.abs(j);
                                abs = abs/60;
                                clockEntity1.setWorkOvertime(String.valueOf(abs));
                            }

                        }

                    }
                clockEntity1.setDate(sdf2.format(clockEntity1.getClockingTime()));
            }
        }
//        return clockMapper.findAll(clockEntity);
        return all;
    }

    @Override
    public ClockEntity findAllByPersonalNameAndClockingTime(ClockEntity clockEntity) {
        return clockMapper.findAllByPersonalNameAndClockingTime(clockEntity);
    }

    @Override
    public List<ClockEntity> findByPersonalNameAndDate(String personalName, String date) {
        return clockMapper.findByPersonalNameAndDate(personalName, date);
    }
}
